package vn.fsoft.gst.dao;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import vn.fsoft.gst.domain.Address;
import vn.fsoft.gst.domain.Contact;
import vn.fsoft.gst.domain.Country;
import vn.fsoft.gst.domain.County;
import vn.fsoft.gst.domain.Organisation;
import vn.fsoft.gst.domain.Sic;

/**
 * 
 * @author ThuanLT
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/testApplicationContext.xml")
@Transactional
public class OrganisationDAOImplTest_ThuanLT {

	@Autowired
	private CountryDAO countryDAO;
	@Autowired
	private CountyDAO countyDAO;
	@Autowired
	private AddressDAO addressDAO;
	@Autowired
	private SicDAO sicDAO;
	@Autowired
	private OrganisationDAO orgDAO;
	@Autowired
	private ContactDAO contactDAO;
	@Before
	public void setUp() throws Exception {
		
	}

	public void Init(){

		Organisation org =  new Organisation();
		org.setAddressLine1("Address line 1");
		org.setOrgName("Org-1");
		org.setOrgShortDescription("This is short desc of Org 1");
		org.setAddressLine1("Address Line 1");
		org.setAddressLine1("9999999999");
		org.setActive(true);
		Address address = new Address();
		
		Country country = new Country();
		country.setCountryName("Country - 1");
		countryDAO.addCountry(country);
		
		County county = new County();
		county.setCountyName("County - 1");
		countyDAO.addCounty(county);
		
		address.setPostCode("70001");
		address.setCountry(country);
		address.setCounty(county);
		addressDAO.addAddress(address);
		
		Sic sic = new Sic();
		sic.setSicCode("12455");
		sic.setTypeOfBussiness("Kinh Doanh");
		sicDAO.addSic(sic);
		
		Contact contact = new Contact();
		contact.setEmail("abc@gmail.com");
		contact.setFirstName("Contact 1");
		contact.setMobile("0123456789");
		contact.setContactID(1);
		contact.setIsActive(true);
		
		contactDAO.addContact(contact);
		
		org.setAddress(address);
		org.setSic(sic);
		org.setContact(contact);		
		orgDAO.addOrganisation(org);		

	}
	
	@After
	public void tearDown() throws Exception {
	}

	@Test
	public void testAddOrganisation() {
		Organisation org =  new Organisation();
		org.setAddressLine1("Address line 1");
		org.setOrgName("Org-1");
		org.setOrgShortDescription("This is short desc of Org 1");
		org.setAddressLine1("Address Line 1");
		Address address = new Address();
		
		Country country = new Country();
		country.setCountryName("Country - 1");
		countryDAO.addCountry(country);
		
		County county = new County();
		county.setCountyName("County - 1");
		countyDAO.addCounty(county);
		
		address.setPostCode("70001");
		address.setCountry(country);
		address.setCounty(county);
		addressDAO.addAddress(address);
		
		Sic sic = new Sic();
		sic.setSicCode("12455");
		sic.setTypeOfBussiness("Kinh Doanh");
		sicDAO.addSic(sic);
		
		Contact contact = new Contact();
		contact.setEmail("abc@gmail.com");
		contact.setFirstName("Contact 1");
		contact.setMobile("0123456789");
		contact.setContactID(1);
		contact.setIsActive(true);
		
		contactDAO.addContact(contact);
		
		org.setAddress(address);
		org.setSic(sic);
		org.setContact(contact);		
		orgDAO.addOrganisation(org);	
		
		Organisation org1;
		List<Organisation> list = orgDAO.getListOrganisation();
		org1 =  list.get(0);
		
		assertEquals("Address Line 1", org1.getAddressLine1());
		
		
	}

	@Test
	public void testEditOrganisation() {
		System.err.println("Begin testEditOrganisation");
		Init();
		List<Organisation> list = orgDAO.getListOrganisation();
		for(Organisation s:list){
			System.err.println(s.getOrgID());
		}
		Organisation org = orgDAO.getOrganisationByID(4);
		org.setPhoneNumber("1234567890");
		orgDAO.updateOrganisation(org);
		org = orgDAO.getOrganisationByID(4);
		assertEquals("1234567890", org.getPhoneNumber());
		
		System.err.println("End testEditOrganisation");
	}

	@Test
	public void testDeleteOrganisation() {
		System.err.println("Begin testDeleteOrganisation");
		Init();
		List<Organisation> list = orgDAO.getListOrganisation();
		assertEquals(1, list.size());
		for(Organisation s:list){
			System.err.println(s.getOrgID());
		}
		orgDAO.deleteOrganisation(1);
		list = orgDAO.getListOrganisation();
		assertEquals(0, list.size());
		
		System.err.println("End testDeleteOrganisation");
	}
	@Test
	public void testListOrganisation() {
		System.err.println("Begin testListOrganisation");
		Init();
		List<Organisation> list = orgDAO.getListOrganisation();
		assertEquals(1, list.size());
		for(Organisation s:list){
			System.err.println(s.getOrgID());
		}
		assertEquals(1, list.size());
		
		System.err.println("End testListOrganisation");
	}


}
